package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.entity.CloudInventory;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.OrderDetail;
import com.serkol.ams.mapper.CloudInventoryMapper;
import com.serkol.ams.modal.SqlFeildModal;
import com.serkol.ams.service.CloudInventoryService;
import com.serkol.constant.AuthConstant;
import com.serkol.handler.MyLambdaQueryWrapper;
import com.serkol.utils.LangValidUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author pingan
 * @description 针对表【cloud_inventory】的数据库操作Service实现
 * @createDate 2022-10-23 11:15:45
 */
@Service
public class CloudInventoryServiceImpl extends ServiceImpl<CloudInventoryMapper, CloudInventory>
        implements CloudInventoryService {

    @Override
    public boolean saveOrder(List<OrderDetail> byOrderDetailList) {
        boolean flag = false;
        List<CloudInventory> list = new ArrayList<>();
        Iterator<OrderDetail> iterator = byOrderDetailList.iterator();
        while (iterator.hasNext()) {
            OrderDetail next = iterator.next();

            MyLambdaQueryWrapper<CloudInventory> queryWrapper = new MyLambdaQueryWrapper<>();
            queryWrapper.eq(CloudInventory::getGoodsId, next.getGoodsId()).eq(CloudInventory::getUserId, next.getUserId());
            CloudInventory one = this.getOne(queryWrapper);
            if (one != null) {
                one.setQuantity(one.getQuantity() + next.getQuantity());
                list.add(one);
            } else {

                CloudInventory cloud = new CloudInventory();
                cloud.setOrderId(next.getOrderId());
                cloud.setUserId(next.getUserId());
                cloud.setGoodsId(next.getGoodsId());
                cloud.setOrderDetailId(next.getId());
                cloud.setQuantity(next.getQuantity());
                list.add(cloud);
            }


        }
        if (list.size() > 0) {
            flag = this.saveOrUpdateBatch(list);
        }

        return flag;
    }
    @Override
    public List<Map<String, Object>> findByMyCloudList() {
        LoginLog loginLog = AuthConstant.getLoginLog();
        Map<String, Object> map = new HashMap<>();
        map.put("ID", "GOODS_ID");
        MyLambdaQueryWrapper<CloudInventory> query = new MyLambdaQueryWrapper<>();
        //
        query.eq(CloudInventory::getUserId,loginLog.getUserId()).select(CloudInventory::getQuantity,CloudInventory::getId).select(CloudInventory::getGoodsId).as("goodsId");
        query.otherTableColmuns("goods", SqlFeildModal.getPicture("IMAGE_ID"), map).as("image");
        query.otherTableColmuns("goods", LangValidUtils.getValid("ug","NAME_UG","NAME"), map).as("name");


        return this.listMaps(query);
    }

    @Override
    public MyPage<Map<String, Object>> findByMyCloudPageList(int pageSize, int current, long userId) {
        LoginLog loginLog = AuthConstant.getLoginLog();
        Map<String, Object> map = new HashMap<>();
        map.put("ID", "GOODS_ID");
        MyLambdaQueryWrapper<CloudInventory> query = new MyLambdaQueryWrapper<>();
        //
        query.eq(CloudInventory::getUserId,userId).gt(CloudInventory::getQuantity,0).select(CloudInventory::getQuantity,CloudInventory::getId).select(CloudInventory::getGoodsId).as("goodsId");
        query.otherTableColmuns("goods", SqlFeildModal.getPicture("IMAGE_ID"), map).as("image");
        query.otherTableColmuns("goods", LangValidUtils.getValid("ug","NAME_UG","NAME"), map).as("name");
        Page<Map<String, Object>> page = this.pageMaps(new Page<>(current, pageSize), query);

        return MyPage.parsePage(page);
    }

    @Override
    public boolean updateOrder(CloudInventory inventory) {
        boolean flag=false;
        MyLambdaQueryWrapper<CloudInventory> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(CloudInventory::getGoodsId,inventory.getGoodsId());
        queryWrapper.eq(CloudInventory::getUserId,inventory.getUserId());
        CloudInventory one = this.getOne(queryWrapper);
        if(one != null){
            one.setQuantity(one.getQuantity()-inventory.getQuantity());
            flag = this.saveOrUpdate(one);
        }
        return flag;
    }

    @Override
    public boolean addOrder(CloudInventory inventory) {
        boolean flag=false;
        MyLambdaQueryWrapper<CloudInventory> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(CloudInventory::getGoodsId,inventory.getGoodsId());
        queryWrapper.eq(CloudInventory::getUserId,inventory.getUserId());
        CloudInventory one = this.getOne(queryWrapper);
        if(one != null){
            one.setQuantity(one.getQuantity()+inventory.getQuantity());
            flag = this.saveOrUpdate(one);
        }else {
            flag = this.save(inventory);
        }
        return flag;
    }
}




